package config;

import java.util.ArrayList;
import java.util.Hashtable;

import main.Common;
import main.Def;
import main.ErrorException;
import script.ScriptJava;

public class EConfig
{
	private ArrayList<String> m_values;
	private Config m_type;
	private String m_text;
	
	public static boolean m_isSimpleFormat = false;
	public static Hashtable<String, Integer> m_ignoreCols = new Hashtable<>();
	public static Hashtable<String, Integer> m_ignoreSheets = new Hashtable<>();
	public static Hashtable<String, Integer> m_preScript = new Hashtable<>();
	
	public static Hashtable<String, ArrayList<String>> m_groups = new Hashtable<>();
	
	public EConfig(String name)
	{
		m_text = name;
		m_type = Config.getConfig(name.toLowerCase());
		m_values = new ArrayList<>();
		if(m_type == null)
		{
			Common.print("no such config " + name);
		}
	}
	
	public static void clearConfig()
	{
		m_isSimpleFormat = false;
		m_ignoreCols.clear();
		m_ignoreSheets.clear();
	}
	
	public Config getType()
	{
		return m_type;
	}
	
	public ArrayList<String> getValues()
	{
		return m_values;
	}
	
	public void addValue(String v)
	{
		m_values.add(v);
	}
	
	public void applyConfig() throws ErrorException
	{
		if(m_type == null || m_values.size() <= 0)
		{
			return;
		}
		switch(m_type)
		{
		case E_SCRIPT_CONFIG:
			applyScript();
			break;
		case E_USE_SIMPLE_FORMAT_CONFIG:
			applySimpleFormat();
			break;
		case E_IGNORE_COLUMN_CONFIG:
			applyIgnoreCol();
			break;
		case E_IGNORE_SHEET_CONFIG:
			applyIgnoreSheet();
			break;
		case E_GROUP_CONFIG:
			applyGroup();
			break;
		case E_PRE_SCRIPT_CONFIG:
			applyPreScript();
			break;
		default:
			break;
		}
	}
	
	private void applyPreScript() throws ErrorException
	{
		for(String str: m_values)
		{
			if(str.length() > 0)
			{
				if(!m_preScript.containsKey(str))
				{
					ScriptJava.loadScript(str);
					m_preScript.put(str, 0);
				}
			}
		}
	}
	
	private void applyGroup()
	{
		int index = m_text.indexOf(Def.CONFIG_SPLIT);
		String name = index > -1? m_text.substring(index + 1): "";
		ArrayList<String> list = m_groups.get(name);
		if(list == null)
		{
			list = new ArrayList<>();
		}
		for(String str: m_values)
		{
			if(str.length() > 0 && !list.contains(str))
			{
				list.add(str);
			}
		}
		m_groups.put(name, list);
	}
	
	private void applyIgnoreSheet()
	{
		for(String str: m_values)
		{
			if(str.length() > 0)
			{
				if(!m_ignoreSheets.containsKey(str))
				{
					m_ignoreSheets.put(str, 0);
				}
			}
		}
	}
	
	private void applyIgnoreCol()
	{
		for(String str: m_values)
		{
			if(str.length() > 0)
			{
				if(!m_ignoreCols.containsKey(str))
				{
					m_ignoreCols.put(str, 0);
				}
			}
		}
	}
	
	private void applySimpleFormat()
	{
		m_isSimpleFormat = m_values.get(0).compareTo(Def.TRUE_STR) == 0;
	}
	
	private void applyScript() throws ErrorException
	{
		for(String str: m_values)
		{
			if(str.length() > 0)
			{
				ScriptJava.loadScript(str);
			}
		}
	}
}
